﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RestSharp;
namespace UI.View.Base.Community
{
    /// <summary>
    /// Com.xaml 的交互逻辑
    /// </summary>
    public partial class Com : Page
    {
        RestSharp.RestClient client = new RestSharp.RestClient(new Uri(Comm.TempData.BaseUrl));

        ViewModel.BuildViewModel vm;
        public   Com()
        {
            InitializeComponent();

            vm = DataContext as ViewModel.BuildViewModel;

            //读取数据
            RestSharp.RestRequest request = new RestRequest("community", Method.GET);

            var resp =   client.Get<ListDataResponse<Model.Community>>(request);
            if(resp.IsSuccessful)
            {
                var list = resp.Data.data;
                vm.CurrCommunities = list.FirstOrDefault();
            }
           

        }

        
        private void Btn_Save_Click(object sender, RoutedEventArgs e)
        {
            if(vm.CurrCommunities ==null)
            {

                return;
            }

            if(string.IsNullOrEmpty(vm.CurrCommunities.Name))
            {
                HandyControl.Controls.Growl.WarningGlobal("小区名称不能为空！");
                return;
            }

            var req = new RestRequest("community",Method.PUT);
            req.AddJsonBody(vm.CurrCommunities);
            var resp = client.Put<SingleDataResponse<int>>(req);
            
            if(resp.IsSuccessful)
            {
                HandyControl.Controls.Growl.SuccessGlobal("保存成功！");
            }else
            {
                HandyControl.Controls.Growl.WarningGlobal(resp.ErrorMessage);
            }
        }
    }
}
